<?php
namespace App\ModelStatistic;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Pagination\LengthAwarePaginator;
use Illuminate\Support\Facades\Request;
use Illuminate\Support\Facades\Input;
use App\Admin\Extra\Search\Html;

class StatisticCommon{

    public static function DisplayRetainNagtive($value, $denominator, $use_persent = false)
    {
        $color = "black";
        $value *= 1;
        if ($value < 0) {
            $value *= -1;
            $color = "red";
        }
        $persent = '';
        if ($use_persent == true){
            if ($denominator == 0)
            {
                $rate = 0;
            }else {
                $rate = round($value/$denominator * 100, 2) ;
            }
            $persent = '%';
        }
        $bg_color_arr = ['','#e6f1f7','#ddf2fd','#cceafb','#b9e5ff','#9ed9fb', '#88d1fb','#333333'];
        switch ($rate) {
            case 0:
                $bg =  $bg_color_arr[0];
            break;
            case $rate <= 5 :
                $bg = $bg_color_arr[1];
            break;
            case $rate <= 10:
                $bg = $bg_color_arr[2];
            break;
            case $rate <= 20:
                $bg = $bg_color_arr[3];
            break;
            case $rate <= 30:
                $bg = $bg_color_arr[4];
            break;
            case $rate <= 40:
                $bg = $bg_color_arr[5];
            break;
            case $rate <= 70:
                $bg = $bg_color_arr[6];
            break;
            case $rate <= 100:
                $bg = $bg_color_arr[7];
                $color = '#ffffff;';
            break;
            default:
                $bg =$bg_color_arr[0];
        }
        return '<div style="color:'.$color.';background:'.$bg.'; min-width:80px;height:100%; text-align:center;padding-top:10px;"> <p>'.$rate.$persent.'</p> <p> '.$value.' </p></div>';
        
        // return "<span style=\"color:$color;\">$value</span>";
    }

    public static function RetainHomeButton($aera, $style = 'day', $param = null)
    {
        return [
            Html::button('next day retain', '/admin/userst/remain?area='.$aera, $style == 'day' ? 'btn-default':'btn-goto btn-info'),
            Html::button('week retain', '/admin/userst/remain-week?area='.$aera,$style == 'week' ? 'btn-default':'btn-goto btn-info'),
            //Html::button('month retain', '/admin/userst/remain-month?area='.$aera,$style == 'month' ? 'btn-default':'btn-goto btn-info')
        ];
    }

    public static function remainChartDate($cond= [], $dely = -8){
        if (is_numeric($cond['start'])) {
            $cond['start'] = date('Y-m-d', $cond['start']);
        }
        $start = $cond['start'] ?$cond['start']: date("Y-m-d", strtotime("$dely day"));

        if (is_numeric($cond['end'])) {
            $cond['end'] = date('Y-m-d', $cond['end']);
        }
        $end = $cond['end'] ?$cond['end']: date("Y-m-d", strtotime('+7 day', strtotime($start)));

        return ['start' => strtotime($start), 'end' => strtotime($end)];
    }

    // 按某个时间  依据开服时间 取得当前周的时间片
    public static function RetainWeekPeriod($area, $start)
    {
        $area_extend = \App\Models\Area::GetAreaExtendInfo($area);
        if (empty($area_extend['open_time'])){
            return ['code' => -301, 'msg' => 'not set area extend open_time;'];
        }
        if (intval($start) !== $start){
            $start = strtotime($start);
        }
        if (empty($start)) {
            $start = strtotime('-7 days');
        }
        if (empty($start) || $start < $area_extend['open_time']){
            $start = intval($area_extend['open_time']);
        }


        $end = $start + 7 * 86400;
        
        return self::remainChartDate(['start' => $start, 'end' => $end]);
    }

    public static function GetIntTime($time_or_date)
    {
        if (intval($time_or_date).'' != $time_or_date){
            $time_or_date = strtotime($time_or_date);
        }
        
        return intval($time_or_date);
    }

    // 自定grid 排序
    public static function SortAble($rw, $def_fld= null, $def_order= null) 
    {
        $param = Input::get('_sort', ['column' => $def_fld, 'type' =>$def_order]);
        if (empty($param['column'])) {
            return ;
        }
        $rw->orderBy($param['column'], $param['type']);
    }

    /**
     * 二维数组按某个字段排序
     * @param $arr
     * @param $field
     * @param int $type SORT_ASC从小到大 SORT_DESC从大到小
     * 另外常用的几个排序函数为 sort rsort asort arsort
     */
    public static function arrayFieldSort(&$arr,$field,$type=SORT_DESC){
        if (empty( $arr)) {
            return $arr;
        }
        $columns = array_column($arr,$field);
        array_multisort($columns,$type,$arr);//根据金额大小降序排列
    }

    public static function getMobieModel() {
        return [ 0 => 'all',"ios" =>'ios', "andriod" => 'andriod',];
    }
}